package com.mic.controller;

import com.mic.pojo.User;
import com.mic.service.AdminService;
import com.mic.service.AdminServiceImpl;
import com.mic.service.UserServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
public class AdminController {
    @Autowired
    @Qualifier("AdminServiceImpl")
    AdminServiceImpl adminService;
    @Autowired
    @Qualifier("UserServiceImpl")
    UserServiceImpl userService;

    @RequestMapping("/searchUser")
    public void searchUser(Model model, @Param("userId") Integer userId, HttpServletRequest request, HttpServletResponse response,
    HttpSession session) throws IOException {
        User user = adminService.queryUserByUserId(userId);
        session.setAttribute("user",user);

        response.sendRedirect(request.getContextPath()+"/toMainAdmin");
    }

    @RequestMapping("/deleteAnArticleByAdmin")
    public void deleteAnArticleByAdmin(HttpServletResponse response, HttpServletRequest request,
                                HttpSession session,Integer certainArticleId) throws IOException {
        userService.deleteAnArticle(certainArticleId);
        response.sendRedirect(request.getContextPath()+"/toMainAdmin");
    }

    @RequestMapping("/deleteUser")
    public void deleteUser(HttpServletResponse response, HttpServletRequest request,
                                       HttpSession session,Integer userId) throws IOException {
        adminService.deleteUser(userId);
        response.sendRedirect(request.getContextPath()+"/toMainAdmin");
    }

}
